package com.he.heblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.he.heblog.dto.UserDto;
import com.he.heblog.entity.Fan;
import com.he.heblog.entity.Role;
import com.he.heblog.entity.User;
import com.he.heblog.entity.UserRole;
import com.he.heblog.mapper.UserMapper;
import com.he.heblog.service.FanService;
import com.he.heblog.service.RoleService;
import com.he.heblog.service.UserRoleService;
import com.he.heblog.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.mail.Session;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 余生
 * @since 2023-03-27
 */
@Service

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService, UserDetailsService {
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    FanService fanService;

/*返回用户信息以及对应的权限信息*/
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        User one = this.getOne(wrapper);
        if(one==null)throw new UsernameNotFoundException("当前用户名不存在");
//        查询用户ID应的角色ID
//        当前不能使用lambda表达式获取列名
        QueryWrapper<UserRole> userWrapper = new QueryWrapper<>();
        userWrapper.eq("userID",one.getId());
        List<UserRole> list = userRoleService.list(userWrapper);
//        获取角色信息集合中的角色ID
        List<Integer> collect = list.stream().map(x -> x.getRoleID()).collect(Collectors.toList());
//根据角色ID查询角色表，获取角色信息
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.in(Role::getId,collect);
        List<Role> roles = roleService.list(roleWrapper);
//       向用户信息中加入角色信息
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(one,userDto);
        userDto.setRoles(roles);
//向会话中保存用户信息
        return userDto;
    }
//    根据ID返回用户的粉丝信息分页
    public Page<User> getUserListPageById(Integer id,Integer start){
//        根据用户ID查询粉丝表，得到其粉丝ID
        QueryWrapper<Fan> fanQueryWrapper = new QueryWrapper<>();
        fanQueryWrapper.eq("otherID",id);
        List<Fan> list = fanService.list(fanQueryWrapper);
        List<Integer> collect = list.stream().map(x -> x.getUserID()).collect(Collectors.toList());
//        根据粉丝ID查询用户表得到用户的详情
        Page<User> userPage = new Page<>(start, 7);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(User::getId,collect);
        Page<User> page = this.page(userPage, wrapper);
        return page;
    }
//   查询用户的关注列表
    public Page<User> getFollowPageById(Integer id,Integer start){
//        根据用户ID查询粉丝表，得到其粉丝ID
        QueryWrapper<Fan> fanQueryWrapper = new QueryWrapper<>();
        fanQueryWrapper.eq("userid",id);
        List<Fan> list = fanService.list(fanQueryWrapper);
        List<Integer> collect = list.stream().map(x -> x.getUserID()).collect(Collectors.toList());
//        根据粉丝ID查询用户表得到用户的详情
        Page<User> userPage = new Page<>(start, 7);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(User::getId,collect);
        Page<User> page = this.page(userPage, wrapper);
        return page;
    }









}
